;(function(){

  const CompInfo = { name: 'AnimeBox', version: 'v1' };
  
  main();

  function main() {
    getYepNope(() => {
      yepnope({
        test: window.axlib,
        nope: ['https://ax.minicg.com/axlib-v3.min.js', 'https://ax.minicg.com/anime.min.js'],
        complete: init
      });
    });
  }

  function init() {
    let repScripts = $('script[type="axure-repeater-template"]');
    $.each(repScripts, (i, k) => {
      let rep = $(k).parent();
      let repId = rep.attr('id');
      let repData = $axure(`#${repId}`).getRepeaterData({format:'row',first:true});

      if ( matchComponent($axure(`#${repId}`).getRepeaterData({format:'row',first:false}), CompInfo.name, CompInfo.version) ) {
        let position = vec2Array(repData['位移']) || [0, 0];
        let rotation = Number(repData['旋转']);
        let scale = Number(repData['缩放']);
        let opacity = Number(repData['透明度']);
        let duration = Number(repData['动画时长']);
        let firstDelay = Number(repData['初始延时']);
        let delay = Number(repData['启动延时']);
        let endDelay = Number(repData['结束延时']);
        let direction = repData['动画方向'] || 'normal';
        let easing = repData['缓动曲线'] || 'easeOutCubic';
        let loop = Number(repData['循环次数']);
        if (loop == -1) { loop = true; }
        let cursorStyle = repData['鼠标样式'] || 'default';
        let pointerEvents = repData['鼠标穿透'].toLowerCase() == 'true' ? 'none' : 'auto';
        let filters = repData['滤镜效果'] || 'none';
        let mixBlendMode = repData['混合模式'] || 'normal';

        setTimeout(()=>{
          anime({
            targets: '#' + repId,
            translateX: Number(position[0]),
            translateY: Number(position[1]),
            rotate: rotation,
            scale,
            opacity,
            easing,
            direction,
            loop,
            duration,
            delay,
            endDelay
          });
        }, firstDelay);

        rep.css({
          'cursor': cursorStyle,
          'pointer-events': pointerEvents,
          'filter': filters,
          'mix-blend-mode': mixBlendMode,
        });
      } else {
        axlib.useToast(()=>{
          console.log('AnimeBox组件版本不匹配!');
          $.toast({
            heading: '数据错误',
            text: 'AnimeBox组件版本不匹配',
            showHideTransition: 'slide',
            icon: 'error'
          });
        });
      }

    });
  }

  function vec2Array(str) {
    str = str.replace('[', '').replace(']', '').replace(' ', '').trim();
    return str.split(',');
  }
  
  function matchComponent(data, name, version) {
    let keyName = '组件信息';
    if (data[keyName] && data[keyName][0]==name && data[keyName][1].indexOf(version) != -1) {
      return true;
    }
    return false;
  }

  function getYepNope(callback) {
    if (window.yepnope) {
      callback();
    } else {
      $.getScript('https://ax.minicg.com/axloader.min.js', callback);
    }
  }
}());